/*
 * DxfLWPOLYLINE.java
 *
 * Created on 5 de Dezembro de 2007, 10:02
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package importadorDXF;

import mathematics.Point3D;

/**
 *
 * @author rafaelbuck
 */
public class DxfLWPOLYLINE extends DxfEntity
{
    public static class LwVertex
    {

        public float getX()
        {
            return x;
        }

        void setX(float x)
        {
            this.x = x;
        }

        public float getY()
        {
            return y;
        }

        void setY(float y)
        {
            this.y = y;
        }

        public Point3D getPosition()
        {
            return new Point3D(x, y, 0.0F);
        }

        public float getStartWidth()
        {
            return startWidth;
        }

        void setStartWidth(float width)
        {
            startWidth = width;
        }

        public float getEndWidth()
        {
            return endWidth;
        }

        void setEndWidth(float width)
        {
            endWidth = width;
        }

        public float getBulge()
        {
            return bulge;
        }

        void setBulge(float bulge)
        {
            this.bulge = bulge;
        }

        private float x;
        private float y;
        private float startWidth;
        private float endWidth;
        private float bulge;

        LwVertex(float width)
        {
            startWidth = endWidth = width;
        }
    }


    public boolean setGroup(short grpNr, float fval)
    {
        switch(grpNr)
        {
        case 10: // '\n'
            vertexNr++;
            if(vertices == null)
                vertices = new LwVertex[1];
            else
            if(vertexNr == vertices.length)
            {
                LwVertex lwv[] = new LwVertex[vertexNr + 1];
                System.arraycopy(vertices, 0, lwv, 0, vertices.length);
                vertices = lwv;
            }
            vertices[vertexNr] = new LwVertex(constantWidth);
            vertices[vertexNr].setX(fval);
            return true;

        case 20: // '\024'
            vertices[vertexNr].setY(fval);
            return true;

        case 40: // '('
            vertices[vertexNr].setStartWidth(fval);
            if((double)fval > 0.0D)
                hasWidth = true;
            return true;

        case 41: // ')'
            vertices[vertexNr].setEndWidth(fval);
            if((double)fval > 0.0D)
                hasWidth = true;
            return true;

        case 42: // '*'
            vertices[vertexNr].setBulge(fval);
            return true;

        case 43: // '+'
            constantWidth = fval;
            if((double)fval > 0.0D)
                hasWidth = true;
            return true;
        }
        
        return super.setGroup(grpNr, fval);
    }

    public boolean setGroup(short grpNr, int ival)
    {
        switch(grpNr)
        {
        case 70: // 'F'
            type = ival;
            return true;

        case 90: // 'Z'
            if(vertices == null && ival > 0)
                vertices = new LwVertex[ival];
            return true;
        }
        return super.setGroup(grpNr, ival);
    }

    public boolean finishRead(DxfFile file)
    {
        if(vertexNr + 1 != vertices.length)
        {
            LwVertex lwv[] = new LwVertex[vertexNr + 1];
            System.arraycopy(vertices, 0, lwv, 0, lwv.length);
            vertices = lwv;
        }
        return super.finishRead(file);
    }

    public void convert(Dxf14Converter converter, DxfFile dxf, Object collector)
    {
        converter.convert(this, dxf, collector);
    }

    public boolean hasLineWidth()
    {
        return hasWidth;
    }

    public final int getType()
    {
        return type;
    }

    public final float getConstantWidth()
    {
        return constantWidth;
    }

    public final LwVertex[] getVertices()
    {
        return vertices;
    }

    /** Creates a new instance of DxfLWPOLYLINE */
    public DxfLWPOLYLINE()
    {
        vertexNr = -1;
    }

    public static final int CLOSED = 1;
    public static final int CONT_LTYPE = 128;
    protected int type;
    protected float constantWidth;
    protected LwVertex vertices[];
    protected int vertexNr;
    protected boolean hasWidth;
}